package com.ruoyi.accountant.service.impl;

import com.ruoyi.web.core.config.AppConf;
import com.ruoyi.accountant.domain.Device;
import com.ruoyi.accountant.domain.DeviceExample;
import com.ruoyi.accountant.domain.Reason;
import com.ruoyi.accountant.mapper.DeviceMapper;
import com.ruoyi.accountant.service.AccountService;
import com.ruoyi.accountant.service.CurrencyService;
import com.ruoyi.accountant.service.DeviceService;
import com.ruoyi.accountant.service.ReasonService;
import com.ruoyi.web.web.form.PayForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    //@Autowired
    private ReasonService reasonService;

    //@Autowired
    private AccountService accountService;

    //@Autowired
    private CurrencyService currencyService;

    //@Override
    public void save(Device device) {
        deviceMapper.insert(device);
    }

    //@Override
    public List<Device> findAll() {
        return deviceMapper.selectByExample(null);
    }

    //@Override
    public void deprecate() {
        // 查询没有完成，但是开始日期小于now（）的记录
        List<Device> deviceList = deviceMapper.findUnfinished();
        for (Device device : deviceList) {
            int deprectedMonth = device.getDeprecatedMonth() + 1;
            if(deprectedMonth == device.getCanUse()) {
                device.setFinished(AppConf.ON);
            }
            device.setDeprecatedMonth(deprectedMonth);

            // 生成支出
            Reason reason = reasonService.findByRname("設備折舊");
            PayForm payForm = new PayForm();
            payForm.setAccount(device.getOfAccount());

            payForm.setTransactionType("非同日支出");
            payForm.setCurrency(device.getOfCurrency());
            payForm.setPayDate(new Date());
            payForm.setProofDate(new Date());
            payForm.setReasonId(reason.getRid());
            payForm.setMoney(device.getMoney().subtract(new BigDecimal(device.getCanUse())).doubleValue());
            payForm.setTaxRateType("零稅率");
            payForm.setTaxRate(0D);

            payForm.setAccountName(accountService.findById(device.getOfAccount()).getAname());
            payForm.setReasonName("設備折舊");
            payForm.setCurrencyName(currencyService.findById(device.getOfCurrency()).getEnName());

            accountService.pay(payForm);

            deviceMapper.updateByPrimaryKeySelective(device);
        }
    }

    @Override
    public List<Device> findUnfinishedByOfAccount(Long aid) {
        DeviceExample deviceExample = new DeviceExample();
        DeviceExample.Criteria criteria =deviceExample.createCriteria();
        if (aid!=null){
            criteria.andOfAccountEqualTo(aid);
        }
        criteria.andFinishedEqualTo(AppConf.OFF);
        return deviceMapper.selectByExample(deviceExample);
    }
}
